package com.the4man.framework.utils;

import net.sf.json.JSON;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import net.sf.json.util.PropertyFilter;
import net.sf.json.xml.XMLSerializer;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by lenovo on 2017/12/12.
 */
public class JsonUtil {
    public JsonUtil() {
    }

    public static JSONObject Object2Json(Object o) {
        return JSONObject.fromObject(o);
    }

    public static JSONArray Object2Json(List o) {
        return JSONArray.fromObject(o);
    }

    public static JSONObject Object2Json(Object o, List filter) {
        JsonConfig jsonConfig = new JsonConfig();
        jsonConfig.setJsonPropertyFilter(new IgnorePropertyFilter(filter));
        return JSONObject.fromObject(o, jsonConfig);
    }

    public static JSONArray Object2Json(List list, List filter) {
        JsonConfig jsonConfig = new JsonConfig();
        jsonConfig.setJsonPropertyFilter(new IgnorePropertyFilter(filter));
        return JSONArray.fromObject(list, jsonConfig);
    }

    public static JSONObject Object2Json(Object obj, PropertyFilter propertyFilter) {
        JsonConfig jsonConfig = new JsonConfig();
        jsonConfig.setJsonPropertyFilter(propertyFilter);
        return JSONObject.fromObject(obj, jsonConfig);
    }

    public static JSONArray Object2Json(List list, PropertyFilter propertyFilter) {
        JsonConfig jsonConfig = new JsonConfig();
        jsonConfig.setJsonPropertyFilter(propertyFilter);
        return JSONArray.fromObject(list, jsonConfig);
    }

    public static String Object2JsonString(Object o) {
        return Object2Json(o).toString();
    }

    public static String Object2JsonString(List o) {
        return Object2Json(o).toString();
    }

    public static String Object2JsonString(Object o, List filter) {
        return Object2Json(o, filter).toString();
    }

    public static String Object2JsonString(List list, List filter) {
        return Object2Json(list, filter).toString();
    }

    public static Object JsonString2Bean(String js, Class c, Map classMap) {
        JSONObject jo = JSONObject.fromObject(js);
        return JSONObject.toBean(jo, c, classMap);
    }

    public static List JsonString2List(String js, Class c, Map classMap) {
        JSONArray array = JSONArray.fromObject(js);
        List l = new ArrayList();
        JSONObject jo = null;
        Object o = null;

        for(int i = 0; i < array.size(); ++i) {
            jo = array.getJSONObject(i);
            o = JSONObject.toBean(jo, c, classMap);
            l.add(o);
        }

        return l;
    }

    public static String JsonString2Xml(String s) {
        XMLSerializer xml = new XMLSerializer();
        String xmlString = xml.write(Object2Json((Object)s));
        return xmlString;
    }

    public static String Xml2JsonString(String s) {
        XMLSerializer xml = new XMLSerializer();
        JSON js = xml.read(s);
        return js.toString();
    }

    public static void main(String[] args) {
    }
}
